package com.runa.persistence;

import com.runa.monitor.comm.entity.CommMeter;
import com.runa.monitor.comm.entity.CommNetEquipment;
import com.runa.monitor.comm.entity.CommPanel;
import com.runa.monitor.comm.entity.CommValve;
import com.runa.persistence.repository.CommMeterRepository;
import com.runa.persistence.repository.CommNetEquipmentRepository;
import com.runa.persistence.repository.CommPanelRepository;
import com.runa.persistence.repository.CommValveRepository;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class RepositoryServicePersis {


    private static CommMeterRepository commMeterRepository;
    private static CommValveRepository commValveRepository;
    private static CommPanelRepository commPanelRepository;

    @Resource
    public  void setCommValveRepository(CommValveRepository commValveRepository) {
        RepositoryServicePersis.commValveRepository = commValveRepository;
    }

    @Resource
    public  void setCommPanelRepository(CommPanelRepository commPanelRepository) {
        RepositoryServicePersis.commPanelRepository = commPanelRepository;
    }

    @Resource
    public void setCommMeterRepository(CommMeterRepository commMeterRepository) {
        RepositoryServicePersis.commMeterRepository = commMeterRepository;
    }

    @Resource
    public void setCommNetEquipmentRepository(CommNetEquipmentRepository commNetEquipmentRepository) {
        RepositoryServicePersis.commNetEquipmentRepository = commNetEquipmentRepository;
    }

    private static CommNetEquipmentRepository commNetEquipmentRepository;

    public static CommNetEquipment getCommNetEquipmentByMeterSerial(String meterSerial) {
//        return commMeterRepository.findOne(Example.of(CommNetEquipment.builder().serial(meterSerial).build()));

        Optional<CommNetEquipment> commNetEquipment = commNetEquipmentRepository.findOne(Example.of(CommNetEquipment.builder().serial(meterSerial).build()));
        return commNetEquipment.orElse(null);
    }

    public static CommNetEquipment getCommNetEquipmentByNetSerial(String serial) {
        Optional<CommNetEquipment> commNetEquipment = commNetEquipmentRepository.findOne(Example.of(CommNetEquipment.builder().serial(serial).build()));
        return commNetEquipment.orElse(null);
    }
    public static CommNetEquipment getCommNetEquipmentOnMac(String mac) {
        Optional<CommNetEquipment> commNetEquipment = commNetEquipmentRepository.findOne(Example.of(CommNetEquipment.builder().mac(mac).build()));
        return commNetEquipment.orElse(null);
    }
    public static CommNetEquipment setCommNetEquipmentOnMac(String serial, String mac) {
        Optional<CommNetEquipment> commNetEquipment = commNetEquipmentRepository.findOne(Example.of(CommNetEquipment.builder().serial(serial).build()));
        CommNetEquipment netEquipment = commNetEquipment.orElse(null);
        netEquipment.setMac(mac);
        commNetEquipmentRepository.save(netEquipment);
        return netEquipment;
    }

//    public static List<CommMeter> getCommMeterListByNetSerial(String serial) {
//        CommNetEquipment commNetEquipment = getCommNetEquipmentByNetSerial(serial);
//        return commNetEquipment.getCommMeterList();
//    }


    public static CommMeter getByNetSerialAndSequence(String netSerial, Integer sequence) {
        return commMeterRepository.getByNetSerialAndSequence(netSerial, sequence);
    }

    public static CommMeter getByNetSerialAndSerialLeftLike(String netSerial, String serial) {
        return commMeterRepository.getByNetSerialAndSerialLeftLike(netSerial, serial);

    }
    public static CommMeter getCommMeterBySerial(String netSerial, String serial) {
        return commMeterRepository.getByNetSerialAndSerial(netSerial, serial);
    }

    public static CommMeter getCommMeterBySerial(String serial) {
        return commMeterRepository.findOne(Example.of(CommMeter.builder().serial(serial).build())).orElse(null);
    }
    public static CommValve getCommValveById(String id) {
        return commValveRepository.findById(id).orElse(null);
    }
    public static CommValve getCommValveByIdAndModelLike(String id, String model) {
        return commValveRepository.findByIdAndModelLike(id, model);
    }

    public static CommValve getCommValveBySerial(String serial) {
        return commValveRepository.findOne(Example.of(CommValve.builder().serial(serial).build())).orElse(null);
    }

    public static CommValve getCommValveByNodeIndexForNetEqu(Integer index, String netIdentity) {
        return commValveRepository.findOne(Example.of(CommValve.builder().sequence(index).netEquipmentId(netIdentity).build())).orElse(null);
    }
    public static CommPanel getCommPanelById(String id) {
        return commPanelRepository.findById(id).orElse(null);
    }

    public static CommPanel getCommPanelByNetIdAndSerial(String netId,String serial) {
        return commPanelRepository.findOne(Example.of(CommPanel.builder().serial(serial).netEquipmentId(netId).build())).orElse(null);
    }
    public static CommPanel getCommPanelByNetIdAndIndex(String netId,Integer index) {
        return commPanelRepository.findOne(Example.of(CommPanel.builder().sequence(index).netEquipmentId(netId).build())).orElse(null);
    }
}
